/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model.statemnt;
/**
 * 说明：
 * 员工管理model
 * @Author：黄盛强
 * @Date：2012-03-20
 */
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.ViewStoreStockSummary;

public class ViewStoreStockSummaryModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Vector<List<Object>> rowdatas=new Vector<List<Object>>();
	private Vector<String> columns = new Vector<String>();
	public ViewStoreStockSummaryModel(){
		columns.add("仓库名称");
		columns.add("仓库数量");
		columns.add("仓库总金额");
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch(columnIndex) {
		case 1:
		case 2:
			return Double.class;
		}
		return super.getColumnClass(columnIndex);
	}
	//获得行数
	public int getRowCount(){
		return rowdatas.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		return ((Vector<Object>)rowdatas.get(row)).get(column);
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//设置单元格是否可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	public void removeData(int row){
		rowdatas.remove(row);
	}
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		//取出对应的记录
		Vector<Object> rowdata = (Vector<Object>)rowdatas.get(rowIndex);
		//删除原来的值
		rowdata.remove(columnIndex);
		//添加用户新修改的值
		rowdata.add(columnIndex,aValue);
	}


	public void setRowData(List<ViewStoreStockSummary> List) {
		rowdatas.clear();
	
		if(List != null && List.size() != 0){
			Collections.sort(List, new ContentComparator());//排序list
			int num = List.get(0).getNum();//统计数量
			double price = List.get(0).getTotalPrice();//统计价格
			String name = List.get(0).getStoreName();//仓库名字
			for(int i = 1;i < List.size();i++){
				ViewStoreStockSummary ViewStoreStockSummary = List.get(i);
				
				if (!name.equals(ViewStoreStockSummary.getStoreName())) {
					List<Object> row=new Vector<Object>();
					row.add(name);
					row.add(num);
					row.add(price);
					rowdatas.add(row);
					
					name = ViewStoreStockSummary.getStoreName();
					num = 0;
					price = 0;
				}
				
				num += ViewStoreStockSummary.getNum();
				price += ViewStoreStockSummary.getTotalPrice();
			}
			
			List<Object> row=new Vector<Object>();
			row.add(name);
			row.add(num);
			row.add(price);
			rowdatas.add(row);
		}
	}
	
	//排序条件
	class ContentComparator implements Comparator<Object> {
		public int compare(Object o1, Object o2) {
			ViewStoreStockSummary object1 = (ViewStoreStockSummary)o1;
			ViewStoreStockSummary object2 = (ViewStoreStockSummary)o2;
			return object1.getStoreName().compareTo(object2.getStoreName());
		}
	}
	
	public void clear() {
		rowdatas.clear();
	}
}

